#!/usr/bin/bash

#   获取时间
time_str="$(date +%Y-%m-%d-%H:%M:%S)"
#   项目名
item_name="bugManagementSystem"
#   服务器地址
server_ip="a-hw-test01"
#   服务器部署路径
server_path="/www/server/tomcat8/webapps"
#   服务器备份路径
backup_path="/www/server/tomcat8/backup"

echo "${time_str}"
echo "项目名: ${item_name}"
echo "服务器地址: ${server_ip}"
echo "部署路径: ${server_path}"
echo "备份路径: ${backup_path}"

read -r -t 300 -p "确定部署? 请输入 yes : " user_input
if [ "${user_input}" != "yes" ];then
echo "用户取消执行..."
exit 1
fi

#   清理
mvn clean
#   打包
mvn package -DskipTests


#   备份项目
sh_str="mv ${server_path}/${item_name}.war ${backup_path}/${item_name}.war.${time_str}
exit"
echo "${sh_str}" | ssh -tt "${server_ip}"

#   等 30 让 tomcat 删除项目文件夹
sleep 30

#   上传新的项目包
scp ./target/${item_name}.war root@"${server_ip}":${server_path}/${item_name}.tmp

#   部署项目
sh_str="mv ${server_path}/${item_name}.tmp ${server_path}/${item_name}.war
exit"
echo "${sh_str}" | ssh -tt "${server_ip}"